#include "socket_can.h"
#include <memory>
#include <unistd.h>
#include <sys/time.h>
#include <iostream>

int main(int argc, char* argv[]) {
  std::shared_ptr<SocketCAN> can_;
  bool use_absolute_timestamp_ = true;
  bool enable_gpio = true;
  bool stop_ = false;
  uint64_t init_time = 0;
  std::string can_name_ = "can0";
  can_ = std::make_shared<SocketCAN>(can_name_, use_absolute_timestamp_);
  can_->Init(enable_gpio);
  struct timeval tv;
  gettimeofday(&tv, NULL);
  init_time = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
  while (!stop_) {
    if (can_ && can_->Update()) {
      if (!use_absolute_timestamp_) {
        can_->can_data.timestamp = can_->can_data.timestamp - init_time;
      }
      std::cout << "id:" << can_->can_data.id << " dlc:" << can_->can_data.dlc
                << " ";
      for (int i = 0; i < can_->can_data.dlc; ++i) {
        printf("%X,", can_->can_data.data[i]);
      }
      std::cout << std::endl;
    }
    usleep(10);
  }
}
